/**
 * 
 */
package com.soccer.cms.common.utils.image;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Positions;

import com.soccer.cms.modules.cms.entity.CropImage;

/**
 * @author zouqone
 * date 2015年5月31日   下午11:27:57
 *
 */
public class ImageUtils {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public static void cropImage(CropImage cropImage,String srcPath,String targetPath) throws IOException{
		File imageFile = new File(srcPath);
		BufferedImage images = ImageIO.read(imageFile );
		Builder<BufferedImage> builder = Thumbnails.of(images);
		
		Integer imageSrcW = images.getWidth();
		Integer imageSrcH = images.getHeight();
		Integer rotate = cropImage.getImageRotate().intValue(); //旋转角度
		
		//旋转图片
		builder.size(imageSrcW, imageSrcH);
		builder.rotate(rotate);
		builder.toFile(targetPath);
		
		//处理旋转后的图片
		File imageFile1 = new File(targetPath);
		BufferedImage images1 = ImageIO.read(imageFile1 );
		builder = Thumbnails.of(images1);
		
		Double rate = cropImage.getImageW()/imageSrcW; //缩放率
		
		//获取未缩放的坐标和宽度
		Double selectorX = (cropImage.getSelectorX()-cropImage.getImageX())/rate ;
		Double selectorY = (cropImage.getSelectorY()-cropImage.getImageY())/rate ;
		Double selectorW = cropImage.getSelectorW()/rate;
		Double selectorH = cropImage.getSelectorH()/rate;
		
		//图片旋转为逆时针，坐标为顺时针，旋转角度加上负号
		Double varX = getVarXByImageXY(imageSrcW,imageSrcH,-rotate);
		Double varY = getVarYByImageXY(imageSrcW,imageSrcH,-rotate);
		
		//获取旋转后的坐标
		selectorX = selectorX-varX.intValue(); 
		selectorY = selectorY-varY.intValue();
		/*System.out.println("Src  X = "+0+" ,Y = "+0+" ,W = "+imageSrcW+" ,H = "+imageSrcH);
		System.out.println("Room X = "+cropImage.getImageX()+" ,Y = "+cropImage.getImageY()+" ,W = "+cropImage.getImageW()+" ,H = "+cropImage.getImageH());
		System.out.println("Org  X = "+(cropImage.getSelectorX())+" ,Y = "+(cropImage.getSelectorY()));
		System.out.println("Room select X = "+(cropImage.getSelectorX()-cropImage.getImageX())+" ,Y = "+(cropImage.getSelectorY()-cropImage.getImageY()));
		System.out.println("Room rate = "+rate+" , test="+imageSrcW*rate);
		System.out.println("varX = "+varX+" ,varY = "+varY);
		System.out.println("selectorX = "+selectorX+" ,selectorY = "+selectorY);*/
		
		builder.sourceRegion(selectorX.intValue(),selectorY.intValue(),selectorW.intValue(),selectorH.intValue() );
		if("1".equals(cropImage.getZoom())){ //按选择尺寸生成
			builder.size(cropImage.getSelectorW().intValue(), cropImage.getSelectorH().intValue());
		}else{ //按原始尺寸生成
			builder.size(selectorW.intValue(), selectorH.intValue());
		}
		
		builder.keepAspectRatio(false);
		builder.toFile(targetPath);
		
	}
	
	/**
	 * 按尺寸缩放
	 * @param srcPath
	 * @param targetPath
	 * @param width
	 * @param height
	 * @throws IOException
	 */
	public static void zoomImageBySize(String srcPath ,String targetPath , Double width, Double height) throws IOException{
		
		File imageFile = new File(srcPath);
		BufferedImage images = ImageIO.read(imageFile );
		Builder<BufferedImage> builder = Thumbnails.of(images);
		
		builder.size(width.intValue(), height.intValue());
		builder.keepAspectRatio(false);
		builder.toFile(targetPath);
	}
	
	/**
	 * 按照比例进行缩放
	 * @param srcPath
	 * @param targetPath
	 * @param scale
	 * @throws IOException
	 */
	public static void zoomImageByScale(String srcPath ,String targetPath ,Double scale) throws IOException{
		
		File imageFile = new File(srcPath);
		BufferedImage images = ImageIO.read(imageFile );
		Builder<BufferedImage> builder = Thumbnails.of(images);
		
		builder.scale(scale);
		builder.keepAspectRatio(true);
		builder.toFile(targetPath);
	}
	
	/**
	 * 通过角度旋转图片
	 * @param srcPath
	 * @param targetPath
	 * @param rotate
	 * @throws IOException
	 */
	public static void zoomImageByRotate(String srcPath ,String targetPath,Double rotate) throws IOException{
		File imageFile = new File(srcPath);
		BufferedImage images = ImageIO.read(imageFile );
		zoomImageByRotate(srcPath, targetPath, images.getWidth(), images.getHeight(), rotate);
	}
	
	/**
	 * 通过宽度、高度、角度旋转图片
	 * @param srcPath
	 * @param targetPath
	 * @param width
	 * @param height
	 * @param rotate
	 * @throws IOException
	 */
	public static void zoomImageByRotate(String srcPath ,String targetPath , Double width, Double height, Double rotate) throws IOException{
		zoomImageByRotate(srcPath, targetPath, width.intValue(), height.intValue(), rotate);
	}
	
	/**
	 * 通过宽度、高度、角度旋转图片
	 * @param srcPath
	 * @param targetPath
	 * @param width
	 * @param height
	 * @param rotate
	 * @throws IOException
	 */
	public static void zoomImageByRotate(String srcPath ,String targetPath , Integer width, Integer height, Double rotate) throws IOException{
		
		File imageFile = new File(srcPath);
		BufferedImage images = ImageIO.read(imageFile );
		Builder<BufferedImage> builder = Thumbnails.of(images);
		
		builder.size(width.intValue(), height.intValue());
		builder.keepAspectRatio(false);
		builder.rotate(rotate);
		builder.toFile(targetPath);
		
	}
	
	
	/**
	 * 获取X坐标变化值
	 * @param imageSrcW
	 * @param imageSrcH
	 * @param rotate
	 * @return
	 */
	public static Double getVarXByImageXY(Integer imageSrcW,Integer imageSrcH, Integer rotate){
		Double varX = null;
		
		double x1,y1,x2,y2,x3,y3,x4,y4;
		
		//原坐标
		x1 = x4 =  -imageSrcW/2.0;
		x2 = x3 =  imageSrcW/2.0;
		y1 = y2 =  imageSrcH/2.0;
		y4 = y3 =  -imageSrcH/2.0;
		
		//旋转后的X坐标
		double[] xArray = new double[4];
		xArray[0] =  rotateX(x1,y1,rotate);
		xArray[1] =  rotateX(x2,y2,rotate);
		xArray[2] =  rotateX(x3,y3,rotate);
		xArray[3] =  rotateX(x4,y4,rotate);
		varX = getMinValueByDoubleArray(xArray);
		return imageSrcW/2.0+varX;
	}
	
	/**
	 * 获取Y坐标变化值
	 * @param imageSrcW
	 * @param imageSrcH
	 * @param rotate
	 * @return
	 */
	public static Double getVarYByImageXY(Integer imageSrcW,Integer imageSrcH, Integer rotate){
		Double varY = null;
		
		double x1,y1,x2,y2,x3,y3,x4,y4;
		
		//原坐标
		x1 = x4 =  -imageSrcW/2.0;
		x2 = x3 =  imageSrcW/2.0;
		y1 = y2 =  imageSrcH/2.0;
		y4 = y3 =  -imageSrcH/2.0;
		
		//旋转后的X坐标
		double[] yArray = new double[4];
		yArray[0] =  rotateY(x1,y1,rotate);
		yArray[1] =  rotateY(x2,y2,rotate);
		yArray[2] =  rotateY(x3,y3,rotate);
		yArray[3] =  rotateY(x4,y4,rotate);
		varY = getMinValueByDoubleArray(yArray);
		return imageSrcH/2.0+varY;
	}
	
	/**
	 * 获取最小值
	 * @param array
	 * @return
	 */
	public static Double getMinValueByDoubleArray(double[] array){
		Double value = null;
		if(array!=null&&array.length>0){
			value = array[0];
			for (int i = 1 ;i<array.length ;i++) {
				if(array[i]<value){
					value = array[i];
				}
			}
		}
		return value;
	}
	
	/**
	 * 计算X坐标旋转rotate角度后的值
	 * @param x0
	 * @param y0
	 * @param rotate
	 * @return
	 */
	public static Double rotateX(Double x0,Double y0 , Integer rotate){
		Double x = null;
		double cosva = Math.cos(Math.toRadians(rotate));
		double sinva = Math.sin(Math.toRadians(rotate));
		x = x0*cosva-y0*sinva;
		return x;
	}
	
	/**
	 * 计算Y坐标旋转rotate角度后的值
	 * @param x0
	 * @param y0
	 * @param rotate
	 * @return
	 */
	public static Double rotateY(Double x0,Double y0 , Integer rotate){
		Double y = null;
		double cosva = Math.cos(Math.toRadians(rotate));
		double sinva = Math.sin(Math.toRadians(rotate));
		y = y0*cosva+x0*sinva;;
		return y;
	}
	
	
	/**
	 * 将字符串转化为整型数字
	 * @param str
	 * @return
	 */
	public static Integer parseInt(String str){
		Long num = Math.round(Double.parseDouble(str));
		
		return num.intValue();
	}
}
